<?php

class DraftsController extends Controller {
    var $name = 'Drafts';
    var $uses = array('Posts','Drafts','Post2category');
    var $components = array('page','json');

    function index() {
        $this->action = 'drafts';
        $this->drafts();
    }

    function drafts($page = 0, $status = null) {
        $user_id = ArlicleUser::user_id();
        $posts = $this->Drafts->get_user_drafts($page, $user_id);
        $this->set('posts', $posts);
        $this->set('page', $page);
        if ($status == 'deleted_done') {
            $this->header_notice = __('Drafts have been deleted.'); 
        }
    }

    function ajax_drafts($page = 0,$ids = null) {
        $this->layout = null;
        $user_id = ArlicleUser::user_id();
        $i = 0;
        if (!empty($ids)) {
            $ids = trim($ids, '|');
            $ids = explode('|', $ids);
            if (!empty($ids)) {
                foreach($ids as $id) {
                    $id = (int)$id;
                    if (!empty($id)) {
                        $this->Posts->del($id);
                        $this->Post2category->del_all("post_id='$id'");
                        $i++;
                    }
                }
            }
        }
        $json_data['deleted_count'] = $i;
        $posts = $this->Drafts->get_user_drafts($page, $user_id);
        $json_data['pagelink'] = $posts->pagelink;
        $json_data['postlist'] = $posts->results;
        $json_data['my_drafts_count'] = current_user_draft_count();
        $json = new Services_JSON();
        echo $json->encode($json_data);
    }
}
?>
